package com.atguigu.gmall.mq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class DelayMqConfig {
    //交换机   交换机内延迟
    //routingKey
    //队列
    public static final String exchange_delay = "exchange.delay";
    public static final String routing_delay = "routing.delay";
    public static final String queue_delay_1 = "queue.delay.1";

    @Bean
    public Exchange exchange1(){
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type","direct");
        return new CustomExchange(exchange_delay,"x-delayed-message",true,false,args);
    }

    @Bean
    public Queue queue1(){
        return QueueBuilder.durable(queue_delay_1).build();
    }

    @Bean
    //qualifier 取自IOC容器，死信中取自当前，一般使用容器中取队列和交换机
    public Binding binding(@Qualifier(value = "queue1") Queue queue,@Qualifier(value = "exchange1") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(routing_delay).noargs();
    }

}
